/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package klijent;

import KLforme.KLogin;
import model.Paket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Radnik;

/**
 *
 * @author Dejan
 */
public class KlijentVeza extends Thread{

    Socket s;
    private ArrayList<Radnik> radnici;
    private ObjectInputStream in;
    private ObjectOutputStream out;
    private boolean prijavljen;
    private String obavestenje;
    private boolean ulogovan;
   // private Object kl;
   private final KLogin kl;
    private String sifra;


    public KlijentVeza(KLogin kl){
        this.kl=kl;

    }

     public void povezi(){
        try {
            s = new Socket("127.0.0.1", 21243);
           


        } catch (UnknownHostException ex) {
            Logger.getLogger(KlijentVeza.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(KlijentVeza.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
     public void prijaviSe(String user, String pass){
     Paket p=new Paket("login");
            p.setUser(user);
            p.setPass(pass);
            posalji(p);

     }


//public void uloguj()
public void posalji(Paket p) {
        try {
            out = new ObjectOutputStream(s.getOutputStream());
            out.writeObject(p);
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(KlijentVeza.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    
  
   public void run() {
        
        //osluskivanje ulaznog toka
        while(s.isConnected()){
            try {
                in = new ObjectInputStream(s.getInputStream());
                
                   otpakuj( (Paket)in.readObject());



            }
            catch(SocketException ex){
                    System.out.println("veza je prekinuta");

                     Logger.getLogger(KlijentVeza.class.getName()).log(Level.SEVERE, null, ex);
                     break;
            }
            catch (ClassNotFoundException ex) {
                    Logger.getLogger(KlijentVeza.class.getName()).log(Level.SEVERE, null, ex);
                
            } catch (IOException ex) {
                Logger.getLogger(KlijentVeza.class.getName()).log(Level.SEVERE, null, ex);
            }


        }

   }
    public Paket upakujRadnika (Radnik r){

        Paket p =new Paket("unos radnika");
        p.setTovar(r);
        return p;
    }

     public void otpakuj(Paket p){
        if(p.getProtokol().equals("sifra radnika")){
            setSifra((String) p.getTovar());

         }

         if(p.getProtokol().equals("lista radnika")){
          radnici=(ArrayList<Radnik>)p.getTovar();

         }
         if(p.getProtokol().equals("login OK")){
         kl.goOnline();
             
             // System.out.println("online");
            //setUlogovan(true);
         }
         
        
              System.out.println(p.getProtokol());

                    
            
   
    }

    /**
     * @return the radnici
     */
    public ArrayList<Radnik> getRadnici() {
        return radnici;
    }

    /**
     * @param radnici the radnici to set
     */
    public void setRadnici(ArrayList<Radnik> radnici) {
        this.radnici = radnici;
    }

    /**
     * @return the ulogovan
     */
    public boolean isUlogovan() {
        return ulogovan;
    }

    /**
     * @param ulogovan the ulogovan to set
     */
    public void setUlogovan(boolean ulogovan) {
        this.ulogovan = ulogovan;
    }

    /**
     * @return the sifra
     */
    public String getSifra() {
        return sifra;
    }

    /**
     * @param sifra the sifra to set
     */
    public void setSifra(String sifra) {
        this.sifra = sifra;
    }


}
